<?php


namespace RedMVC\CodeGenerator;
use RedMVC;

/**
 * PhpClass class
 *
 * @category    RedMVC
 * @package     CodeGenerator
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class PhpClass{

    protected $_viewScript;

    protected $_namespace;
    protected $_uses        = array();
    protected $_name;
    protected $_extends;
    protected $_constants   = array();
    protected $_properties  = array();
    protected $_methods     = array();

    public function __construct(){
        $this->_viewScript = LIB_PATH.DS.'RedMVC'.DS.'CodeGenerator'.DS.'views'.DS.'phpClass.phtml';
    }

    public function setNamespace($namespace){
        $this->_namespace = $namespace;
    }

    public function addUse($use){
        $this->_uses[] = $use;
    }

    public function setName($name){
        $this->_name = $name;
    }

    public function setExtends($extends){
        $this->_extends = $extends;
    }

    public function addConstant(PhpClassConstant $constant){
        $this->_constants[] = $constant;
    }

    public function addProperty(PhpClassProperty $property){
        $this->_properties[] = $property;
    }

    public function addMethod(PhpClassMethod $method){
        $this->_methods[] = $method;
    }

    public function render(){
        ob_start();
        include($this->_viewScript);
        return ob_get_clean();
    }

    public function save($fileName){
        $content = "<?php\n\n";
        $content .= $this->render();
        $fp = fopen($fileName, 'w');
        if(!$fp){
            throw new \Exception('CodeGenerator: cannot write to file');
        }
        fwrite($fp, $content);
        fclose($fp);
    }
}